/**
 * MIT License
 *
 * Copyright (c) 2024 小村程序员
 * Email: 397237738@qq.com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.open.cobertura.core.model;


import org.open.cobertura.data.CoberturaCoverageCounter;

import java.util.BitSet;

/**
 * 有状态指令集
 */
public class InstructionStateful {
    /**
     * 指令对应的行号，映射指令和行号的关系
     */
    private final int lineNumber;

    /**
     * 分支总数
     */
    public int branchesCount;

    /**
     * 覆盖分支总数
     */
    public final BitSet coveredBranchesCount;

    /**
     * 上一个指令
     */
    private InstructionStateful previousInstruction;

    /**
     * 上一个分支
     */
    private int previousBranch;

    public InstructionStateful(int lineNumber) {
        this.lineNumber = lineNumber;
        this.coveredBranchesCount=new BitSet();
        this.branchesCount=0;
    }

    /**
     * 添加分支，如果分支被覆盖，状态也传递到前一个分支
     * @param target
     * @param branchesCount
     */
    public void setBranch(InstructionStateful target, int branchesCount){
        this.branchesCount++;
        target.previousInstruction=this;
        target.previousBranch=branchesCount;
        if(!target.coveredBranchesCount.isEmpty()){
            setExecuteBranch(target,branchesCount);
        }
    }

    /**
     * 设置分支
     * @param executed
     * @param branchesCount
     */
    public void setBranch(final boolean executed,final int branchesCount){
        this.branchesCount++;
        if(executed){
            setExecuteBranch(this,branchesCount);
        }
    }

    private static void setExecuteBranch(InstructionStateful instructionStateful, int branchesCount){
        while(instructionStateful!=null){
            if(!instructionStateful.coveredBranchesCount.isEmpty()){
                instructionStateful.coveredBranchesCount.set(branchesCount);
                break;
            }
            instructionStateful.coveredBranchesCount.set(branchesCount);
            branchesCount=instructionStateful.previousBranch;
            instructionStateful=instructionStateful.previousInstruction;
        }
    }

    /**
     * 返回指令对应的行
     * @return
     */
    public int getLineNumber() {
        return lineNumber;
    }


    /**
     * 得到指令总数
     * @return
     */
    public CoberturaCoverageCounter getInstructionCount(){
        CoberturaCoverageCounter coberturaCoverageCounter=new CoberturaCoverageCounter();
        if(coveredBranchesCount.isEmpty()){
            coberturaCoverageCounter.setCoveredCount(0);
            coberturaCoverageCounter.setMissedCount(1);
        }else{
            coberturaCoverageCounter.setCoveredCount(1);
            coberturaCoverageCounter.setMissedCount(0);
        }
        return coberturaCoverageCounter;
    }

    /**
     * 得到分支总数
     * @return
     */
    public CoberturaCoverageCounter getBranchCount(){
        CoberturaCoverageCounter coberturaCoverageCounter=new CoberturaCoverageCounter();
        if(branchesCount<2){
            coberturaCoverageCounter.setCoveredCount(0);
            coberturaCoverageCounter.setMissedCount(0);
        }else{
            final int covered=coveredBranchesCount.cardinality();
            coberturaCoverageCounter.setMissedCount(branchesCount-covered);
            coberturaCoverageCounter.setCoveredCount(covered);
        }
        return coberturaCoverageCounter;
    }
}
